let router = require('../router/Router');
let questionService = require('../service/questionService');

const { BusiError, SysError } = require('../exception/exception');


router.post("/auth/question/ask", (req, resp) => {
    if (!req.body.question || req.body.question === ""){
        console.logw("提问内容为空", req.uid);
        resp.send({code: 500, msg: "提问内容不能为空"});
    }

    questionService.saveQuestion({uid: req.uid, question: req.body.question}).then(() => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("ask question err:", err);
        resp.send({code: 500, msg: "系统繁忙, 请稍后再试"});
    });
});

router.post("/auth/question/query", (req, resp) => {
    questionService.queryQuestionByUid({uid: req.uid}).then((res) => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("ask question err:", err);
        resp.send({code: 500, msg: "系统繁忙, 请稍后再试"});
    });
});

router.post("/auth/question/read", (req, resp) => {
    if (!req.body.id) {
        resp.send({code: 500, msg: "id有误"});
        return;
    }

    questionService.setQuestionReadByIdAndUid({id:req.body.id, uid: req.uid}).then(() => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("ask question err:", err);
        resp.send({code: 500, msg: "系统繁忙, 请稍后再试"});
    });
});